var jtpl = (function(){
	return {
		delimiterLeft:"\\[\\[", // 分界符号
		delimiterRight:"\\]\\]",
		parseIfSyntax:function(content, obj){
			return content
					.replace(
						new RegExp("@if\\(" + this.delimiterLeft + "([^\\[\\]]*?)" + this.delimiterRight + "\\)([^@]*?)@else([^@]*?)@/if", 'igm'), 
						function($, $1, $2, $3){
							return (typeof(obj[$1]) !== 'undefined' && obj[$1]) ? $2 : $3;
						});
		},
		replaceVars:function(html, obj){// 变量替换
			if(typeof(obj) != 'object') return '';

			return html
					.replace(
						new RegExp(this.delimiterLeft + "([^\\[\\]]*?)" + this.delimiterRight, 'igm'), 
						function($,$1){
							return obj[$1]?obj[$1]:$;
					});
		},
		parse:function(){
			var html= arguments[0] || '',
				obj = arguments[1] || {};
			html = this.parseIfSyntax(html, obj);
			return this.replaceVars(html, obj);
		}
	}
}());

// 调用方式
var html = document.getElementById('entry').innerHTML;
var source = jtpl.parse(html, {title:'chuanshanjia', body:'shiti', name:'zhoubc'});
document.getElementById('test').innerHTML = source;
console.log(source);
